using UnityEngine;
using UnityEditor;

namespace LCH
{
    /// <summary>
    /// 网格绘制器 - 碰撞体相关功能模块
    /// </summary>
    public partial class LchMeshPainter
    {
        /// <summary>
        /// 检查建筑树碰撞体（用于放置树木等对象）
        /// </summary>
        void CheckBuildTreeCollider()
        {
            Vector2 mousePosition = Event.current.mousePosition;
            int id = GUIUtility.GetControlID(FocusType.Passive);
            Vector3 point = Vector3.zero;

            //int id = GUIUtility.GetControlID(FocusType.Passive);
            treeRay = HandleUtility.GUIPointToWorldRay(mousePosition);

            
            treeBrushHit = Physics.Raycast(treeRay, out treeHit, 5000f, -1);
            SphereCapPos(treeHit.point, treeBrushSize, treeBrushHit);


            
            if (null != treeObj)
            {
                if (null != Event.current)
                {
                    
                    if (Event.current.button == 0)
                    {
                        if (Event.current.type == EventType.MouseDown)
                        {
                            if (Event.current.control)
                            {
                                DeleteObject(treeHit.point);
                            }
                            else
                            {
                                AddObject(treeRay, treeHit.point, treeHit.distance, treeHit.normal);
                            }

                            Event.current.Use();
                        }
                        else if (Event.current.type == EventType.MouseUp)
                        {
                            
                            Event.current.Use();
                        }
                        else if (Event.current.type == EventType.MouseMove)
                        {
                            Event.current.Use();
                        }
                        else if (Event.current.type == EventType.MouseDrag)
                        {
                            if (Event.current.control)
                            {
                                DeleteObject(treeHit.point);
                            }
                            else
                            {
                                AddObject(treeRay, treeHit.point, treeHit.distance, treeHit.normal);
                            }
                            Event.current.Use();
                        }
                    }
                }
               
            }
        }
        
        /// <summary>
        /// 检查并创建网格碰撞体（用于射线检测）
        /// </summary>
        void CheckMeshCollider()
        {
            if (lastSelectObject != selectObject)
            {
                ReleaseMeshCollider();
                GameObject g = new GameObject("Collider Mesh");
                g.hideFlags = HideFlags.DontSaveInEditor| HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy;
                MeshCollider mc = g.AddComponent<MeshCollider>();
                mc.sharedMesh = selectObject.sharedMesh;
                mc.convex = false;
                selectMeshCollider = mc;
                lastSelectObject = selectObject;
                if (null == brushMat2)
                    brushMat2 = new Material(Shader.Find("Hidden/BrushToUV1"));
                MeshFilter mf = g.AddComponent<MeshFilter>();
                mf.sharedMesh = selectObject.sharedMesh;
                selectMeshRender = g.AddComponent<MeshRenderer>();
            }
            if (null != blendMat)
            {
                if (isDragging)
                {
                    blendMat.SetColor("_Color", setting.drawingColor);
                }
                else
                {
                    blendMat.SetColor("_Color", setting.brushColor);
                }
            }
        }
        
        /// <summary>
        /// 释放网格碰撞体资源
        /// </summary>
        void ReleaseMeshCollider()
        {
            if (null != selectMeshRender)
                GameObject.DestroyImmediate(selectMeshRender.gameObject, true);
            lastSelectObject = null;

             
        }

        /// <summary>
        /// 修正碰撞体位置，使其与选择对象同步
        /// </summary>
        void FixColliderPosition()
        {
            if (null != selectMeshCollider)
            {
                selectMeshCollider.transform.parent = selectObject.transform.parent;
                selectMeshCollider.transform.position = selectObject.transform.position;
                selectMeshCollider.transform.localRotation = selectObject.transform.localRotation;
                selectMeshCollider.transform.localScale = selectObject.transform.localScale;
            }
        }
    }
}

